package com.hyg.lib_music.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.Net.CommonHttpUtil;
import com.hyg.lib_music.adapter.knowlegeCategotyAdapter;
import com.hyg.lib_music.databinding.FragmentKnowledgeSort1Binding;
import com.hyg.lib_music.modle.knowledgeCategoryData;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class KnowledgeSort1Fragment extends Fragment {

    FragmentKnowledgeSort1Binding binding;
    BaseActivity activity = (BaseActivity) getActivity();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        binding = FragmentKnowledgeSort1Binding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    public void init() {
        CommonHttpUtil.getKnowLedgeCategoryData(1,new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        activity.ErrorDialog("服务器错误");
                    }
                });
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Log.i("lgb", "onResponse: "+result);
                knowledgeCategoryData articData = JSONObject.parseObject(result, knowledgeCategoryData.class);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (articData.getCode() == 200) {
                            initArticList(articData.getData());
                        } else {
                            activity.ErrorDialog(articData.getMessage());
                        }
                    }
                });
            }
        });
    }

    public void initArticList(List<knowledgeCategoryData.DataDTO> data) {
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        binding.rcvKnowledge.setLayoutManager(layoutManager);
        knowlegeCategotyAdapter adapter=new knowlegeCategotyAdapter(getActivity(),data);
        binding.rcvKnowledge.setAdapter(adapter);
    }

}